﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows;
using System.Diagnostics;

namespace AVCWizard
{
    public class Language
    {
        string _desc;
        string _id;

        public Language(string desc, string id)
        {
            _desc = desc;
            _id = id;
        }

        public override string ToString()
        {
            return _desc;
        }

        public override int GetHashCode()
        {
            return _id.GetHashCode();
        }

        public string Desc { get { return _desc; } }
        public string Id { get { return _id; } }
    }

    public static class DictionaryLoader
    {
        public static Dictionary<string, string> LoadDictionaryFromXml(string fileName, string langId)
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            try
            {
                XDocument xdoc = XDocument.Load(fileName);
                XElement root = xdoc.Root;
                if (!root.Name.LocalName.Equals("dictionaries"))
                    throw new ArgumentException("Wrong root tag!");
                foreach (var langNode in root.Elements())
                {
                    if (langNode.Name.LocalName.Equals("dictionary"))
                    {
                        if (langNode.Attribute("id").Value.Equals(langId))
                        {
                            foreach (var text in langNode.Elements())
                            {
                                if (text.Name.LocalName.Equals("text"))
                                {
                                    string key = text.Attribute("key").Value;
                                    dict.Add(key, text.Value);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.StackTrace);
                MessageBox.Show("Can't load language file", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return null;
            }
            return dict;
        }

        public static Language[] LoadLanguages(string fileName)
        {
            List<Language> lang = new List<Language>();
            try
            {
                XDocument xdoc = XDocument.Load(fileName);
                XElement root = xdoc.Root;
                if (!root.Name.LocalName.Equals("dictionaries"))
                    throw new ArgumentException("Wrong root tag!");
                foreach (var langNode in root.Elements())
                {
                    if (langNode.Name.LocalName.Equals("dictionary"))
                    {
                        Language newLang = new Language(langNode.Attribute("desc").Value, langNode.Attribute("id").Value);
                        lang.Add(newLang);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.StackTrace);
                MessageBox.Show("Can't load language file", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return null;
            }
            return lang.ToArray();
        }
    }
}
